package io.renren.modules.app.utils;

/**
 * 邀请码与二维码工具类
 */
public class CodeUtils {

    final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
            '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
            'N', 'P', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z' };

    /**
     * @param str
     * @return true:为空 false:非空
     */
    public static boolean isEmpty(String str) {
        return str == null || "".equals(str);
    }
    
    public static void main(String[] args) {
        // System.out.println(hasChineseCharacter(escapeChar4Solr("gm")));
        System.out.println(geneInviteCodeById(212, 200));
        System.out.println(geneInviteCodeById(212, -200));
        System.out.println(geneInviteCodeById(-212, -200));
        /*
         * String tt = "公司(集团)"; System.out.println(escapeChar4Solr(tt));
         */
        // System.out.println(geneInviteCodeById(10000*10000,100));
        // System.out.println(geneInviteCodeById(10000*10000-1,100));
        // System.out.println(geneInviteCodeById(999,8000000));
    }

    public static String transUniCode2Chinese(String source) {
        StringBuilder sb = new StringBuilder();
        int i = -1;
        int pos = 0;
        while ((i = source.indexOf("\\u", pos)) != -1) {
            sb.append(source.substring(pos, i));
            if (i + 5 < source.length()) {
                pos = i + 6;
                sb.append((char) Integer.parseInt(
                        source.substring(i + 2, i + 6), 16));
            }
        }
        sb.append(source.substring(pos));
        return sb.toString();
    }

    public static String geneInviteCodeById(int x, int y) {
        long mm = x * 999983;
        if (mm > 1000000000) {
            mm = mm / 10;
        }
        long m = mm + y;
        // System.out.println(m);
        String ab = numericToString(m, 32);
        if (ab.length() < 6) {
            for (int i = 6 - ab.length(); i > 0; i--) {
                ab = 0 + ab;
            }
        }
        return ab;
    }

    public static String numericToString(long i, int system) {
        long num = 0;
        if (i < 0) {
            num = ((long) 2 * 0x7fffffff) + i + 2;
        } else {
            num = i;
        }
        char[] buf = new char[32];
        int charPos = 32;
        while ((num / system) > 0) {
            buf[--charPos] = digits[(int) (num % system)];
            num /= system;
        }
        buf[--charPos] = digits[(int) (num % system)];
        return new String(buf, charPos, (32 - charPos));
    }

}
